package com.gym.code.config.admin.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gym.code.config.admin.bean.method.MethodBaseMappingBean;
import com.gym.code.config.admin.bean.method.MethodBaseMappingBean.MethodResultType;
import com.gym.code.config.admin.bean.method.MethodColumnMappingBaseBean;
import com.gym.code.config.admin.bean.method.MethodColumnMappingBaseBean.FunctionMappingType;
import com.gym.code.config.admin.bean.table.TableBaseMappingBean;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.service.IMethodColumnMappingService;
import com.gym.code.config.admin.service.IMethodMappingService;
import com.gym.code.config.admin.service.ITableMappingService;
import com.gym.code.generator.EnumContants.MethodOperateType;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 */
@Controller
@RequestMapping("/method")
public class MethodController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ITableMappingService tableMappingService;
	
	
	@Autowired
	private IMethodMappingService mappingService;
	
	@Autowired
	private IMethodColumnMappingService methodColumnMappingService;
	
	
	@RequestMapping("/pAdd")
	public ModelAndView pAdd(long tableId) {
		TableMappingBean tableBean = tableMappingService.query(tableId);
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("bean", tableBean);
		return new ModelAndView("/method/add",model);
	}
	
	@RequestMapping("/add")
	public ModelAndView add(long tableId,String methodName,int methodOperateType,int methodParameterType,int methodResultType,HttpServletRequest request) {
		logger.info("method add ");
		
		TableBaseMappingBean tableBaseBean = tableMappingService.queryBase(tableId);
		
		MethodBaseMappingBean methodBaseBean = new MethodBaseMappingBean();
		methodBaseBean.setTableId(tableId);
		methodBaseBean.setFunctionName(methodName);
		methodBaseBean.setMethodOperateType(methodOperateType);
		methodBaseBean.setMethodParameterType(methodParameterType);
		if(methodOperateType == MethodOperateType.select.getId()){
			methodBaseBean.setMethodResultType(methodResultType);
		} else {
			methodBaseBean.setMethodResultType(MethodResultType.update);
		}
		mappingService.insert(methodBaseBean);
		
		
		Enumeration<?> names = request.getParameterNames();
		List<String> parameterColumnList = new ArrayList<String>();
		List<String> resultColumnList = new ArrayList<String>();
		List<String> updateColumnList = new ArrayList<String>();
		while(names.hasMoreElements()){
			String name = (String)names.nextElement();
			if(name.endsWith("_parameter")){
				parameterColumnList.add(name.split("_")[0]);
			}else if(name.endsWith("_result")){
				resultColumnList.add(name.split("_")[0]);
			}else if(name.endsWith("_update")){
				updateColumnList.add(name.split("_")[0]);
			}
		}
		
		long methodId = methodBaseBean.getId();
		
		if(CollectionUtils.isNotEmpty(parameterColumnList)){
			List<MethodColumnMappingBaseBean> parameterBeanList = new ArrayList<MethodColumnMappingBaseBean>();
			for(String item : parameterColumnList){
				MethodColumnMappingBaseBean bean = new MethodColumnMappingBaseBean();
				bean.setMappingType(FunctionMappingType.parameter);
				bean.setMethodId(methodId);
				bean.setColumnId(Long.parseLong(item));
				bean.setBeginParameterName(request.getParameter(item+ "_parameter_begin"));
				bean.setEndParameterName(request.getParameter(item+ "_parameter_end"));
				if(StringUtils.isNotEmpty(request.getParameter(item+ "_listFlag"))){
					bean.setListFlag(Integer.parseInt(request.getParameter(item+ "_listFlag")));
				}
				parameterBeanList.add(bean);
			}
			methodColumnMappingService.insert(parameterBeanList);
		
		}
		
		//查询还需要处理下结果集
		if(methodOperateType == MethodOperateType.select.getId()){
			List<MethodColumnMappingBaseBean> resultBeanList = new ArrayList<MethodColumnMappingBaseBean>();
			for(String item : resultColumnList){
				MethodColumnMappingBaseBean bean = new MethodColumnMappingBaseBean();
				bean.setMappingType(FunctionMappingType.result);
				bean.setMethodId(methodId);
				bean.setColumnId(Long.parseLong(item));
				resultBeanList.add(bean);
			}
			methodColumnMappingService.insert(resultBeanList);
		} else if(methodOperateType == MethodOperateType.update.getId()){
			List<MethodColumnMappingBaseBean> updateBeanList = new ArrayList<MethodColumnMappingBaseBean>();
			for(String item : updateColumnList){
				MethodColumnMappingBaseBean bean = new MethodColumnMappingBaseBean();
				bean.setMappingType(FunctionMappingType.update);
				bean.setMethodId(methodId);
				bean.setColumnId(Long.parseLong(item));
				updateBeanList.add(bean);
			}
			methodColumnMappingService.insert(updateBeanList);
		
		}
		
		return new ModelAndView("redirect:/table/view.do?tableId=" + tableId + "&tableName=" + tableBaseBean.getTableName());
	}
	
}
